import Vue from 'vue';
// import urls from '../aixos/urls.js';
const prefix='base-demo-ui';

function setStorage (key, value,type) {
	key = `${prefix}-${key}`;
	if(type=='local'){
		localStorage.setItem(key, JSON.stringify(value))
	}
	if(type=='session'){
		sessionStorage.setItem(key, JSON.stringify(value))
	}
}
function getStorage (key,type) {
	key = `${prefix}-${key}`;
	if(type=='local'){
		var value=localStorage.getItem(key);
	}
	if(type=='session'){
		var value=sessionStorage.getItem(key);
	}
	if (value && value!=='undefined') {
	      return JSON.parse(value)
	}else{
		return ''
	}
}

function removeStorage (key,type) {
    key = `${prefix}-${key}`
	if(type=='local'){
		localStorage.removeItem(key)
	}
    if(type=='session'){
    	sessionStorage.removeItem(key)
    }
}
Vue.prototype.$removeStorage= removeStorage;

function clearStorage () {
    let len = localStorage.length
    let keys = []
    for (let i = 0; i < len; i++) {
      let key = localStorage.key(i)
      if (key && key.startsWith(prefix)) {
        keys.push(key)
      }
    }
    keys.map(key => localStorage.removeItem(key))
}
Vue.prototype.$clearStorage = clearStorage;

export  {
	setStorage,
	getStorage,
}
